package org.ytor.core.sqlflow.dsl.eval.function.support;

import org.ytor.core.sqlflow.dsl.expr.node.support.FunctionCallNode;
import org.ytor.core.sqlflow.dsl.eval.function.Func;

import java.util.Collection;
import java.util.List;

/**
 * created by yangtong on 2025/8/6 23:48:32
 * <br/>
 * 获取字符串或数组长度
 */
public class Length implements Func {

    @Override
    public Boolean support(FunctionCallNode functionCallNode) {
        return functionCallNode.functionName.equalsIgnoreCase("length");
    }

    @Override
    public Object call(List<Object> args) {
        if (args.size() != 1) {
            throw new IllegalArgumentException("length函数参数只能有一个：length(arr|string)");
        }
        Object arg = args.getFirst();
        if (arg instanceof Collection) return ((Collection<?>) arg).size();
        if (arg instanceof String) return ((String) arg).length();
        throw new RuntimeException("Unsupported type for length(): " + arg);
    }
}
